/* Copyright 2009 CtrlSpace Platform
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Contributor(s):
 * 	Marc Obaldo (marcklaser@gmail.com)
 */

dojo.provide("ctrlspace.ui.editor.EditorManager");
dojo.require("ctrlspace.ui.editor.EditorPane");
dojo.declare("ctrlspace.ui.editor.EditorManager", dijit.layout.TabContainer, {
	// Attributes
	openedFiles: {},

	postCreate: function(/* Object */ args) {
		this.inherited(arguments);
	},
	
	getEditor: function(path) {
		return this.openedFiles[path];
	},
	
	// Methods
	closeEditor: function(editor) {
		delete this.openedFiles[editor.projectId + "_" + editor.path];
	},	

	openEditor: function(projectId, path, title, iconClass) {
		var editor = this.getEditor(projectId + "_" + path);
		if (editor ==  null) {
			IDE.getUI().setStatus("Opening " + title + "...", ctrlspace.config.STATUS_PROGRESS);
			var newEditor = new ctrlspace.ui.editor.EditorPane({
				title: title,
				iconClass: iconClass,
				closable: true,
				path: path,
				projectId: projectId
			});

			dojo.addClass(newEditor.domNode, 'csLoading');

			this.addChild(newEditor);
			this.selectChild(newEditor);
			this.openedFiles[projectId + "_" + path] = newEditor;

			var xhr = dojo.xhrGet({
				url: ctrlspace.config.projectManagerUrl,
				content: {
					command: "open_file",
					projectId: projectId,
					filePath: path
				},
				handleAs: 'json'
			});
	
			xhr.addCallback(dojo.hitch(this, this.openEditorSuccess));
			return;
		} else {
			this.selectChild(editor);
		}
	},
	
	saveEditor: function(editor) {
		var saveRequest = dojo.xhrPost({
			url: ctrlspace.config.projectManagerUrl,
			content: {
				command: "save_file",
				projectId: editor.projectId,
				filePath: editor.path,
				contents: editor.getEditor().getCode()
			},
			load: dojo.hitch(this, this.saveEditorSuccess),
			handleAs: "json",
			sync: true
		});
	},

	// Callbacks
	openEditorSuccess: function(response) {
		var textArea = document.createElement('textarea');
			textArea.id = response.id;

		var editor = this.getEditor(response.projectId + "_" + response.path);
		var contentArea = new CodeMirror(editor.domNode, {
			id: response.id + '_iframe',
			content: response.content,
			parserfile: "parsejava.js",
			stylesheet : "js/ctrlspace/codemirror/css/javacolors.css",
			path : "js/ctrlspace/codemirror/",
			textWrapping: false,
			indentUnit: 4,
			tabMode: "shift",
			width: "100%",
			height: "100%",
			onChange: dojo.hitch(editor, editor.onEditorChange),
			cursorActivity: dojo.hitch(editor, editor.onCursorActivity)
		});
		
		this.selectChild(editor);
		editor.setEditor(contentArea);
		dojo.connect(this, "selectChild", editor, editor.onSelected);
		dojo.removeClass(editor.domNode, 'csLoading');
		IDE.getUI().setStatus(response.label + " opened", ctrlspace.config.STATUS_SUCCESS, 5);
	},
	
	saveEditorSuccess: function(file) {
		var editor = this.getEditor(file.projectId + "_" + file.path);
		editor.setIsDirty(false);
	}
});